package com.qiaofang.applet.service.property.service.impl;

import com.qiaofang.applet.common.context.QfContext;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.QiNiuUtil;
import com.qiaofang.applet.constants.WaterMarkConstants;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.domain.PropertyWaterMark;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.property.service.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: youqing.yang
 * @Create: 2021/03/26 14:58
 * @Description:
 **/
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private CorporationService corporationService;

    @Override
    public String propertyWaterMark(String photoUrl, PropertyWaterMark propertyWaterMark) {
        if (StringUtils.isBlank(photoUrl)) {
            return "";
        }

        if (propertyWaterMark == null || !StringUtils.equals(propertyWaterMark.getWatermark(), WaterMarkConstants.ON)) {
            return photoUrl;
        }
        String returnUrl = photoUrl;
        if (StringUtils.equals(WaterMarkConstants.TEXT_WATERMARK, propertyWaterMark.getWatermarkType())) {
            returnUrl = QiNiuUtil.watermarkSynthesisText(photoUrl, propertyWaterMark.getWatermarkContent());
        }
        if (StringUtils.equals(WaterMarkConstants.PHOTO_WATERMARK, propertyWaterMark.getWatermarkType()) && StringUtils.isNotBlank(propertyWaterMark.getWatermarkUrl())) {
            returnUrl = QiNiuUtil.watermarkSynthesis(photoUrl, propertyWaterMark.getWatermarkUrl(), propertyWaterMark.getWatermarkAlign());
        }
        return returnUrl;
    }

    @Override
    public boolean isV20Corporation() {
        String corporationCode = QfContextHolder.getContext().getCorporationCode();
        if (StringUtils.isBlank(corporationCode)) {
            throw new BusinessException("从上下文获取集团code失败");
        }
        Corporation corporation = corporationService.getCorporationByCorporationCode(corporationCode);
        if (corporation == null) {
            throw new BusinessException("集团不存在~");
        }
        return corporation.getV20Flag() ? true : false;
    }
}
